package com.cpi.cornpp.creditlinebalance.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.creditlinebalance.converter.CreditLineBalanceConverter;
import com.cpi.cornpp.creditlinebalance.dao.CreditLineBalanceDAO;
import com.cpi.cornpp.creditlinebalance.dto.CreditLineBalanceDTO;
import com.cpi.cornpp.creditlinebalance.exception.CreditLineBalanceException;
import com.cpi.cornpp.creditlinebalance.view.CreditLineBalanceView;

public class CreditLineBalanceService {

	public List list(int creditLineId, int initPeriodId, int finishPeriodId) throws CreditLineBalanceException{
		List listDtos = null;
		List listViews = null;
		CreditLineBalanceDAO dao = null;
		CreditLineBalanceConverter converter = null;
		dao = new CreditLineBalanceDAO();
		try{
			dao = new CreditLineBalanceDAO();
			converter = new CreditLineBalanceConverter();
			listDtos = new ArrayList(0);
			listViews = new ArrayList(0);
			// Obtengo el listado
			listDtos = dao.list(creditLineId, initPeriodId, finishPeriodId);
			// Mando convertir el listado en la vista de retorno
			listViews = converter.convertDTOs(listDtos);		
			
		} catch (CreditLineBalanceException clbException) {			
			clbException.printStackTrace(System.out);
			clbException= new CreditLineBalanceException(clbException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw clbException;
		}
		catch (Exception e){
			CreditLineBalanceException creditLineBalanceException=null;
			e.printStackTrace(System.out);
			creditLineBalanceException= new CreditLineBalanceException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw creditLineBalanceException;
		}
		
		return listViews;
	}
	
	public void update(List balances) throws CreditLineBalanceException{
		Iterator it1 = balances.iterator();
		CreditLineBalanceDTO dto = null;
		CreditLineBalanceConverter converter = null;
		CreditLineBalanceDAO dao = null;
		
		converter = new CreditLineBalanceConverter();
		dao = new CreditLineBalanceDAO();
		try{
			while (it1.hasNext()) {
				CreditLineBalanceView element = (CreditLineBalanceView) it1.next();
				if( element.isMonthlyPeriodApplied() ){
					dto = converter.convert(element);
					if( element.isBalanceEmpty() ){
						dao.insert(dto.getCreditLineId(), dto.getMonthlyPeriodId(), dto.getBalance());
					}else{
						dao.update(dto.getCreditLineId(), dto.getMonthlyPeriodId(), dto.getBalance());
					}
				}
			}
		}catch (CreditLineBalanceException clbException) {			
			clbException.printStackTrace(System.out);
			clbException= new CreditLineBalanceException(clbException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw clbException;
		}
		catch (Exception e){
			CreditLineBalanceException creditLineBalanceException=null;
			e.printStackTrace(System.out);
			creditLineBalanceException= new CreditLineBalanceException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw creditLineBalanceException;
		}
	}
	
}
